package com.ruoyi.web.controller.modules;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.mapper.SysDictDataMapper;
import com.sdstc.modules.eneity.PersonEntity;
import com.sdstc.modules.service.OrgService;
import com.sdstc.modules.service.PersonService;
import com.sdstc.modules.vo.OrgTreeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * @author ：wk
 * @date ：Created in 2022/10/23 13:29
 */
@RestController
@RequestMapping("/person")
public class PersonController extends BaseController {

    @Autowired
    private PersonService personService;

    @Autowired
    private SysDictDataMapper sysDictDataMapper;

    @Autowired
    private OrgService orgService;

    private final String jobLevelDict[] = {"law_enforcement", "general_management", "police_skills"};

    @PostMapping
    public AjaxResult savePerson(@RequestBody PersonEntity person){
        PersonEntity personEntity = personService.savePerson(person);
        return AjaxResult.success(personEntity);
    }

    @PutMapping("/{id}")
    public AjaxResult updatePerson(@PathVariable String id,@RequestBody PersonEntity person){
        PersonEntity personEntity = personService.updatePerson(person);
        return AjaxResult.success(personEntity);
    }

    @DeleteMapping("/{id}")
    public AjaxResult deletePersonById(@PathVariable String id){
        personService.deletePersonById(id);
        return AjaxResult.success();
    }

    @GetMapping("/list")
    public TableDataInfo getPersonPage(PersonEntity person){
        List<SysDictData> jobLevelDictData = sysDictDataMapper.selectList(new QueryWrapper<SysDictData>().in("dict_type", Arrays.asList(jobLevelDict)));
        Map<String, OrgTreeVo> orgId2Map = orgService.getOrgId2Map(person.getOrgId());
        startPage();
        return getDataTable(personService.getPersonPage(person, jobLevelDictData, orgId2Map));
    }

    /**
    *@author: wk
    *@date: 2023/2/22 14:35
    *@description: 通过excel导入人员
    */
    @PostMapping(value = "/import/{orgId}")
    public void importPersonByExcel(HttpServletResponse response, @RequestParam("file") MultipartFile file, @PathVariable String orgId) throws IOException {
        personService.importPersonByExcel(response,file,orgId);
    }

    /**
    *@author: wk
    *@date: 2023/9/22 16:26
    *@description: 新模板导入
    */
    @PostMapping(value = "/importNew/{orgId}")
    public void importPersonByNewExcel(HttpServletResponse response, @RequestParam("file") MultipartFile file, @PathVariable String orgId) throws IOException {
        personService.importPersonByNewExcel(response,file,orgId);
    }
    
    /**
    *@author: wk
    *@date: 2023/12/8 14:33
    *@description: 人员学历统计
    */
    @GetMapping(value = "/getEducationChart")
    public AjaxResult getEducationChart() {
        return AjaxResult.success(personService.getEducationChart());
    }

    @PostMapping("/exportEducationExcel")
    public void exportYearExcel(HttpServletResponse response){
        personService.exportEducationExcel(response);
    }
    
    /**
    *@author: wk
    *@date: 2023/12/8 14:50
    *@description: 年龄分布统计
    */
    @GetMapping(value = "/getAgeChart")
    public AjaxResult getAgeChart() {
        return AjaxResult.success(personService.getAgeChart());
    }

    @PostMapping("/exportAgeExcel")
    public void exportAgeExcel(HttpServletResponse response){
        personService.exportAgeExcel(response);
    }

}
